// packageC/pages/package/gonzhuangronqiguanli/5S_manage/5S_handling/handling.js
const mybehavior = require("../../../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    fs_upload_url: "https://logisticsengineering.gree.com/self_form_backend/photo_manage",
    baseUrl: "https://logisticsengineering.gree.com/self_form_backend/form_application_manage",
    page: 1,
    page_size: 10,
    activeNamesDetail: ['1'],
    activeNamesStep: ['1'],
    steps: [
    ],
    active: 0,
    now_steps: {},

    btn_list: [],
    files: []

  },
  // 上传照片
  getUploaderVlaue(e) {
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
  },

  // 展开详情
  onActiveDetailChange(event) {
    this.setData({
      activeNamesDetail: event.detail,
    });
  },
  // 展开步骤
  onActiveChangeStep(event) {
    this.setData({
      activeNamesStep: event.detail,
    });
  },
  // 处理签审日志数据
  handleStep() {
    let live_record1 = JSON.parse(JSON.stringify(this.data.task_data.task.live_record))
    for (let i in live_record1) {
      live_record1[i].time = this.GMTToStr(live_record1[i].time)
    }
    let step = []
    for (let i in live_record1) {
      let obj = {
        // text: live_record1[i].action,
        desc: live_record1[i].involved_name + "，" + live_record1[i].time + "，" + live_record1[i].action,
        detail: live_record1[i]
      }
      step.push(obj)
    }
    this.setData({
      steps: step,
      active: live_record1.length - 1
    })


  },
  // 点击记录步骤详情弹窗显示
  showStep(e) {
    let step_arr = JSON.parse(JSON.stringify(this.data.steps[e.detail].detail))
    let arr = []
    if (step_arr.files != undefined) {
      for (let i = 0; i < step_arr.files.length; i++) {
        let obj = {
          url: this.data.fs_upload_url + '/fs_download?file_id=' + step_arr.files[i],
          isImage: true,
          file_id: step_arr.files[i]
        }
        arr.push(obj)
        

      }
    step_arr.files = arr
  }
    this.setData({
      now_steps: step_arr,
      step_detail_show: true
    })

  },
  // 点击记录步骤详情弹窗关闭
  onStepClose() {
    this.setData({
      now_steps: {},
      step_detail_show: false,
      date_show1: false,
      date: "",

    })
  },
  // 处理步骤状态按钮显示
  handleStats() {
    let status = this.data.task_data.status
    let map_arr = this.data.map
    let map_keys = Object.keys(this.data.map)
    for (let i = 0; i < map_keys.length; i++) {
      if (map_keys[i] == status) {
        this.setData({
          btn_list: map_arr[map_keys[i]]
        })
      }

    }
  },
  // 处理按钮
  handleBtn(e) {
    console.log(e);
    let key_str = e.currentTarget.dataset.key_str

    if (key_str == "报完工") {
      this.setData({
        zgwg_show: true,
        action: "report",
      })
    }
    if (key_str == "同意") {
      this.setData({
        action: "agree",
        zgwg_show: true
      })
    }
    if (key_str == "驳回") {
      this.setData({
        action: "disagree",
        zgwg_show: true
      })
    }

  },
  // 整改弹窗确认
  onConfirm1() {
    if (this.data.reason == "" || this.data.files.length == 0) {
      return wx.showToast({
        title: '说明照片不能为空',
        icon: 'none'
      })
    } else {
      let url = this.data.baseUrl + "/update_request"
      let img_arr = []
      if (this.data.files.length != 0) {
        for (let i = 0; i < this.data.files.length; i++) {
          img_arr.push(this.data.files[i].file_id);
        }
      }
      let data = {
        key: this.data.task_data.task_id,
        form_data: this.data.task_data.task.form_data,
        current_node_id: this.data.task_data.task.current_node_id,
        current_involved: this.data.current_involved,
        current_node_name: this.data.task_data.task.current_node_name,
        form_data: this.data.task_data.task.form_data,
        action: this.data.action,
        files: img_arr,
        recipient_id: "",
        reason: this.data.reason,
      }
      console.log(data);
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
          })
          if (this.data.task_data.flag == "1") {
            setTimeout(function () {
              wx.reLaunch({
                url: '/packageB/pages/package/task_manager/task_manager',
              })
            }, 2000)
          } else {
            setTimeout(function () {
              wx.reLaunch({
                url: '/packageC/pages/package/gonzhuangronqiguanli/nanjin_manager/nav_nanjing_handling/nav_nanjing_handling',
              })
            }, 2000)
          }
        } else {

          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {

        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }
  },
  // 整改弹窗关闭
  onHandleClose1() {
    this.setData({
      zgwg_show: false,
      reason: "",
      action: "",
      files: [],

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      current_involved: wx.getStorageSync('_id')
    })
    let self = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log("传过来的数据", data)
      if (data.task.current_node_name == "baowangong") {
        self.setData({
          btn_list: ['报完工'],
        })
      }
      if (data.task.current_node_name == "shenpi" || data.task.current_node_name == "shenpi_power") {
        self.setData({
          btn_list: ['同意', '驳回'],
        })
      }

      self.setData({
        task_data: data,
      })
      // 处理签审日志
      self.handleStep()

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})